<?php
namespace app\admin\controller;
use think\Db;
use think\Controller;
header("content-type:text/html;charset=utf-8");
class Goods extends Controller{
    //商品列表展示
    public function goodslist(){
        $sql = "select * from (man_goods INNER JOIN man_merchants on man_goods.m_id = man_merchants.m_id) INNER JOIN man_brand on man_goods.b_id = man_brand.b_id";
        $data = Db::query($sql);
        return view('goodslist',['data'=>$data]);
    }
    //商品详情
    public function details(){
        $g_id = input('get.g_id');
        $sql = "select a_id,a_name from man_attr_value where g_id =$g_id";
        $attr_value = Db::query($sql);
        $attr_value=$attr_value;

        foreach ($attr_value as $key=>$val){
            $sql = "select a_id,a_name from man_attr where a_id=".$val['a_id'];
            $attr1 = Db::query($sql);
            $attr[] = $attr1['0'];
        }

        foreach ($attr as $k=>$v){
            $v=join(',',$v); //降维,也可以用implode,将一维数组转换为用逗号连接的字符串
            $temp[$k]=$v;
        }
        $attr=array_unique($temp); //去掉重复的字符串,也就是重复的一维数组
        foreach ($attr as $k => $v){
            $array=explode(',',$v); //再将拆开的数组重新组装
            //下面的索引根据自己的情况进行修改即可
            $temp2[$k]['a_id'] =$array[0];
            $temp2[$k]['a_name'] =$array[1];
        }
        $array = [];
        foreach($temp2 as $key=>$val){
            $array[$key] = $val;
            $aaa = [];
            foreach ($attr_value as $k=>$v){
                if($val['a_id'] == $v['a_id']){
                    $aaa[] = $v;
                }
            }
            $array[$key]['aa_id'] = $aaa;
        }
//var_dump($array);die;

        $sql = "select * from man_goods inner join man_category on man_goods.c_id = man_category.c_id where g_id='$g_id'";
        $goods = Db::query($sql);
        $imgsql = "select * from man_goodsimg where g_id='$g_id'";
        $img = Db::query($imgsql);


        return view('details',['goods'=>$goods,'img'=>$img,'array'=>$array]);
    }
    //详情的图片添加
    public function addimg(){
        $g_id = input('post.g_id');
        $g_url = $_FILES['g_url'];
        $path = 'upload/'. time() .$g_url['name'];
        move_uploaded_file($g_url['tmp_name'],$path);
        $addsql = "insert into man_goodsimg values ('','$g_id','$path',1,1,1)";
        $addres = Db::execute($addsql);
        if ($addres){
            $this->redirect('admin/goods/goodslist');
        }
    }
    //商品删除
    public function goodsdel(){
        $id = $_GET['g_id'];
        $res = Db::table('man_goods')->where('g_id='.$id)->delete();
        if ($res){
            $this->redirect('admin/goods/goodslist');
        }
    }
    //商品状态修改为不推荐
    public function zt()
    {
        $id = input('get.g_id');
        $g_statc = input('get.p');
        $sql = "update man_goods set g_statc=0 where g_id='$id'";
        $res = Db::execute($sql);
        if ($res)
        {
            return 1;
        }
    }
    //商品状态修改为推荐
    public function zt1()
    {
        $id = input('get.g_id');
        $g_statc = input('get.p');
        $sql = "update man_goods set g_statc=1 where g_id='$id'";
        $res = Db::execute($sql);
        if ($res)
        {
            return 1;
        }
    }
    //商品热卖修改为不热卖
    public function re()
    {
        $id = input('get.g_id');
        $g_is_hot = input('get.p');
        $sql = "update man_goods set g_is_hot=0 where g_id='$id'";
        $res = Db::execute($sql);
        if ($res)
        {
            return 1;
        }
    }
    //商品不热卖修改为热卖
    public function bre()
    {
        $id = input('get.g_id');
        $g_is_hot = input('get.p');
        $sql = "update man_goods set g_is_hot=1 where g_id='$id'";
        $res = Db::execute($sql);
        if ($res)
        {
            return 1;
        }
    }
    //商品新品修改为不新品
    public function isnew()
    {
        $id = input('get.g_id');
        $g_is_hot = input('get.p');
        $sql = "update man_goods set g_is_new=0 where g_id='$id'";
        $res = Db::execute($sql);
        if ($res)
        {
            return 1;
        }
    }
    //商品不新品修改为新品
    public function bnew()
    {
        $id = input('get.g_id');
        $g_is_hot = input('get.p');
        $sql = "update man_goods set g_is_new=1 where g_id='$id'";
        $res = Db::execute($sql);
        if ($res)
        {
            return 1;
        }
    }
    //商品不下架修改为下架
    public function isdel()
    {
        $id = input('get.g_id');
        $g_is_hot = input('get.p');
        $sql = "update man_goods set g_is_del=0 where g_id='$id'";
        $res = Db::execute($sql);
        if ($res)
        {
            return 1;
        }
    }
    //商品下架修改为不下架
    public function bdel()
    {
        $id = input('get.g_id');
        $g_is_hot = input('get.p');
        $sql = "update man_goods set g_is_del=1 where g_id='$id'";
        $res = Db::execute($sql);
        if ($res)
        {
            return 1;
        }
    }
    //商品添加页面
    public function addgoods(){
        $csql = "select * from man_category";
        $category = Db::query($csql);
        $data = $this->createTree($category);
        $msql = "select * from man_merchants";
        $mdata = Db::query($msql);
        $bsql = "select * from man_brand";
        $bdata = Db::query($bsql);
        $tsql = "select * from man_type";
        $tdata = Db::query($tsql);
        $type_id = input('get.type_id');
        if (!empty($type_id)){
            $attrsql = "select a_id,a_name from man_type,man_attr where man_type.type_id=man_attr.type_id and man_type.type_id=$type_id";
            $attr = Db::query($attrsql);
            if($attr){
                return json_encode($attr);
                die;
            }
        }
        return view('addgoods',['cate'=>$data,'mdata'=>$mdata,'bdata'=>$bdata,'tdata'=>$tdata]);
    }
    public function typedata(){
        $c_id = input('get.c_id',1);
        $tsql = "select * from man_type where c_id=$c_id";
        $tdata = Db::query($tsql);
        return json_encode($tdata);

    }
    //商品添加入库
    public function addgoodsdo(){
        $data = input();
//        var_dump($data);die;
//        var_dump($data);exit;
        $time = time();
        $g_url = $_FILES['g_url'];
        $path = 'upload/'. time() .$g_url['name'];
        move_uploaded_file($g_url['tmp_name'],$path);
        $goodssql = "insert into man_goods values ('','".$data['g_name']."','".$data['m_id']."','".$data['c_id']."','".$data['b_id']."','".$data['g_content']."','$time',0,'".$data['g_number']."','".$data['g_num']."','".$data['g_intro']."',0,0,'".$data['g_is_del']."',0,0,'$path','$data[g_price]')";
//        echo $goodssql;die;
        $res = Db::execute($goodssql);
        $sql = "select g_id from man_goods order by g_addtime desc";
        $goodsdata = Db::query($sql);
        $g_id = $goodsdata['0']['g_id'];

        $attrsql = "insert into man_attr_value values ";
        $values = '';
        foreach ($data['a_name'] as $v){
            $arr['a_name'][] = $v;
        }
        foreach ($data['a_price'] as $v){
            $arr['a_price'][] = $v;
        }
//        var_dump($arr);die;
        for($i = 0; $i<count($arr['a_name']); $i++){
            $name = $arr['a_name'][$i];
            $price = $arr['a_price'][$i];
            $values .= "(null,$data[a_id],$g_id,'$name',$price),";
        }
//        echo $values;die;
        $values = rtrim($values,',');
        $attrsql .= $values;
        $attrres = Db::execute($attrsql);
        if ($res&&$attrres){
            $this->redirect('admin/goods/goodslist');
        }
    }
    //递归
    function createTree($info,$parent_id=0,$level=0){
        // 定义静态的数组
        static $newArr = array();
        // 处理
        foreach ($info as $key => $v) {
            // 判断
            if($v['c_pid'] == $parent_id){
                // 将level放入到数组
                $v['level'] = $level;
                // 把v赋给新数组
                $newArr[] = $v;
                // 调用自己
                $this -> createTree($info,$v['c_id'],$level+1);
            }
        }
        // 数组返回
        return $newArr;
    }



}